iT邦幫忙

2024 iThome 鐵人賽

DAY 9
0
Python

一起來用 Snakify 練練手系列 第 9

【一起來用 Snakify 練練手】Day9 Lesson 3 練習題參考解答-3

  • 分享至 

  • xImage
  •  

接下來就把剩下的題目分享一下

  • Distance to closest point
    ✨ 計算線上兩點距離只要分析一個軸,所以應該很簡單
a = int(input())
b = int(input())
c = int(input())

dis_1 = abs(a-b)
dis_2 = abs(a-c)

if dis_1 < dis_2:
    print(dis_1)
else:
    print(dis_2)
  • Digits in ascending order
    ✨ 判斷數值是否逐漸變大
a = int(input())

num_100 = a//100
num_10 = (a//10)%10
num_1 = a%10

if num_1>num_10 and num_10>num_100:
    print('YES')
else:
    print('NO')
  • Four-digit palindrome
    ✨ 回文數判斷
a = int(input())
num_12 = str(a//100)
num_34 = str(a%10) + str(a//10%10) 

if num_12 == num_34:
    print('YES')
else:
    print('NO')
  • Index of outlier
    ✨ 判斷不一樣數值在哪個位置
a = int(input())
b = int(input())
c = int(input())

if a!=b and b==c:
    print(1)
elif a!=b and a==c:
    print(2)
else:
    print(3)
  • Chocolate bar
a = int(input())
b = int(input())
c = int(input())

if a!=1 and b!=1:
    if (c%a==0 or c%b==0) and (c//a<b or c//b<a):
        print('YES')
    else:
        print('NO')
else:
    print('NO')
  • Leap year
    ✨ 閏年
a = int(input())
if (a%4==0 and a%100!=0) or (a%400==0):
    print('LEAP')
else:
    print('COMMON')
  • Days in month
    ✨ 每個月的天數
month = int(input())
if month == 2:
    print(28)
elif month == 4 or month == 6 or month == 9 or month == 11:
    print(30)
else:
    print(31)
  • Next day
    ✨ 明天幾月幾號
a = int(input())
b = int(input())
o_1 = 0
o_2 = 0

if a==2:
    if b == 28:
        o_1 = a+1
        o_2 = 1
    else:
        o_1 = a
        o_2 = b+1
elif a==4 or a==6 or a==9 or a==11:
    if b == 30:
        o_1 = a+1
        o_2 = 1
    else:
        o_1 = a
        o_2 = b+1
else:
    if b == 31:
        o_2 = 1
        if a==12:
            o_1 = 1
        else:
            o_1 = a+1
    else:
        o_1 = a
        o_2 = b+1
        
print(o_1)
print(o_2)
  • Linear equation
a = int(input())
b = int(input())
if a == 0 and b == 0:
    print('many solutions')
elif a == 0 and b != 0 or b % a != 0:
    print('no solution')
else:
    print(b // a)
  • Vertices of rectangle
    ✨ 找出最後一個點
a = int(input())
b = int(input())
c = int(input())
d = int(input())
e = int(input())
f = int(input())
o_x = 0
o_y = 0

if a==c and a!=e:
    o_x = e
else:
    if a==e:
        o_x = c
    else:
        o_x = a
    
if b==d and b!=f:
    o_y = f
else:
    if b==f:
        o_y = d
    else:
        o_y = b
    
print(o_x)
print(o_y)
  • Sort three numbers
    ✨ 排序
a = int(input())
b = int(input())
c = int(input())

o1 = 0
o2 = 0
o3 = 0

if c>a and c>b:
    o3 = c
    if a>b:
        o1, o2 = b, a
    else:
        o1, o2 = a, b

elif b>a and b>c:
    o3 = b
    if a>c:
        o1, o2 = c, a
    else:
        o1, o2 = a, c
else:
    o3 = a
    if b>c:
        o1, o2 = c, b
    else:
        o1, o2 = b, c

print(o1)
print(o2)
print(o3)

上一篇
【一起來用 Snakify 練練手】Day8 Lesson 3 練習題參考解答-2
下一篇
【一起來用 Snakify 練練手】Day10 Lesson 4 概念
系列文
一起來用 Snakify 練練手21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言